﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls;
using VINETLIFE;
using VINETLIFE.BL;
using VINETLIFE.DO;

namespace VINETLIFE.Module
{
    public partial class ucVnHeader : System.Web.UI.UserControl
    {
        cmsProductCategoryBL cateBL = new cmsProductCategoryBL();
        private Cache cache = HttpContext.Current.Cache;
        protected void Page_Load(object sender, EventArgs e)
        {
            rptMainMenuDataSource();
            if (!IsPostBack)
            {
                sysConfigBL configBL = new sysConfigBL();
                bindToDropDown(ddlLocation);
                if (Session["LocationId"] != null)
                {
                    int outLocationId;
                    bool isNumber;

                    isNumber = int.TryParse(Session["LocationId"].ToString(), out outLocationId);
                    if (isNumber)
                    {
                        ddlLocation.SelectedValue = outLocationId.ToString();
                    }
                }
                ltrHotline.Text = configBL.Select(new sysConfigDO { ConfigID = 6 }).ConfigValue;
                h1Title.InnerText = configBL.Select(new sysConfigDO { ConfigID = 34 }).ConfigValue;
            }
            if (Session["CustomerId"] != null)
            {
                if (Session["UserName"] != null)
                {
                    ltrLogin.Text = Session["UserName"].ToString();
                }
            }
            else
            {
                ltrLogin.Text = "Đăng nhập";
            }
            if (Session["shop"] == null)
            {
                tcartdetail.Visible = false;
            }
            else
            {
                tcartdetail.Visible = true;
                //
                try
                {
                    MyShopCart cart = new MyShopCart();
                    cart = (MyShopCart)Session["shop"];
                    if (cart.GetTotalProduct() <= 0)
                    {
                        tcartdetail.Visible = false;
                    }
                    else
                    {
                        ltrNumber.Text = cart.GetTotalProduct().ToString() + " sản phẩm - ";
                        ltrTotal.Text = string.Format("{0:0,0}", cart.GetTotalCost()) + " đ";
                    }
                }
                catch { }
            }
        }
        public void bindToDropDown(DropDownList ddl)
        {

            DataTable dtCate = new cmsLocationBL().SelectAll();
            foreach (DataRow dr in dtCate.Rows)
            {
                if (dr["ParentID"].ToString().Equals("0"))
                {
                    ListItem item = new ListItem();
                    item.Value = dr["LocationId"].ToString();
                    item.Text = dr["Location"].ToString();
                    ddl.Items.Add(item);
                    GetChildItems(dr["LocationId"].ToString(), dtCate, ddl);
                }
            }
        }
        private void GetChildItems(string parentID, DataTable dtTemp, DropDownList ddl)
        {
            foreach (DataRow dr in dtTemp.Rows)
            {
                if (dr["ParentID"].ToString().Equals(parentID))
                {
                    ListItem listChild = new ListItem();
                    listChild.Text = Server.HtmlDecode("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;") + dr["Location"].ToString();
                    listChild.Value = dr["LocationId"].ToString();
                    ddl.Items.Add(listChild);
                }
            }
        }
        protected void rptMainMenuDataSource()
        {
            rptMainMenu.DataSource = cateBL.SelectRoot();
            rptMainMenu.DataBind();
        }


        protected void rptMainMenu_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DataRowView drv = (DataRowView)e.Item.DataItem;

                Repeater rptMainMenuChild = (Repeater)e.Item.FindControl("rptMainMenuChild");
                if (rptMainMenuChild != null)
                {
                    int parentId = int.Parse(drv["ProductCategoryId"].ToString());
                    rptMainMenuChild.DataSource = cateBL.SelectByParent(parentId);
                    rptMainMenuChild.DataBind();
                }
            }
        }
        public string FriendlyUrl(string input)
        {
            return Ultility.Change_AV(input);
        }
        protected void ddlLocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            Ultility.ClearAllCache();
            Session["LocationId"] = ddlLocation.SelectedValue;
            // Session["LocationCode"] = 
            Response.Redirect(HttpContext.Current.Request.Url.AbsolutePath);
        }
    }
}